const fs = require("fs");

function deletedir(dir){

    let arr = fs.readdirSync(dir);

    arr.forEach(item =>{
        let url = dir+"/"+item;
        let stat = fs.statSync(url)
        if(stat.isFile()){
            //如果是文件直接删除
            fs.unlinkSync(url)

        }else{//如果是目录重复以上动作
            deletedir(url)
        }
    })

    fs.rmdirSync(dir);
}

deletedir("./testdir");